<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人博客</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body class="bg-gray-100 flex flex-col min-h-screen">
    <header class="bg-white shadow-md">
        <div class="container mx-auto px-4 py-4 flex justify-between items-center">
            <h1 class="text-2xl font-bold text-blue-600">个人博客</h1>
            <div>
                {% if current_user.is_authenticated %}
                <div class="flex items-center space-x-4">
                    <a href="{{ url_for('profile') }}" class="flex items-center text-gray-700 hover:text-primary">
                        <img src="{{ current_user.get_avatar(30) }}" alt="{{ current_user.nickname }}" class="w-8 h-8 rounded-full mr-2">
                        <span>{{ current_user.nickname or current_user.email.split('@')[0] }}</span>
                    </a>
                    <a href="{{ url_for('logout') }}" class="bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-3 rounded text-sm">
                        登出
                    </a>
                </div>
                {% else %}
                <div class="space-x-2">
                    <a href="{{ url_for('login') }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-3 rounded text-sm">
                        登录
                    </a>
                    <a href="{{ url_for('register') }}" class="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-3 rounded text-sm">
                        注册
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </header>
    <div class="flex-grow container mx-auto px-4 py-8">
        <header class="text-center mb-10">
            <h1 class="text-[clamp(2rem,5vw,3rem)] font-bold text-dark mb-2">个人博客系统</h1>
            <div class="mt-6">
                {% if current_user.is_authenticated %}
                <a href="{{ url_for('new_post') }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded inline-flex items-center">
                    <i class="fas fa-plus-circle mr-2"></i>写新文章
                </a>
                {% else %}
                <a href="{{ url_for('login') }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded inline-flex items-center">
                    <i class="fas fa-sign-in-alt mr-2"></i>登录后发布文章
                </a>
                {% endif %}
            </div>
        </header>

        <div class="space-y-6">
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                {% for post in posts %}
                <article class="post-card bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow transform hover:scale-105 transition-transform duration-200 p-6">
                    <h2 class="text-2xl font-bold mb-2">
                        <a href="{{ url_for('view_post', post_id=post.id) }}" class="text-dark hover:text-primary transition-colors">
                            {{ post.title }}
                        </a>
                    </h2>
                    <div class="flex items-center text-gray-500 text-sm mb-4">
                        <span class="flex items-center">
                            <i class="fa fa-calendar mr-1"></i>
                            {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}
                        </span>
                        <span class="mx-2">•</span>
                        <span class="flex items-center">
                            <i class="fa fa-comments mr-1"></i>
                            {{ post.comments|length }} 条评论
                        </span>
                    </div>
                    <div class="prose max-w-none mb-4">
                        {{ post.content|truncate(200) }}
                    </div>
                    <div class="flex justify-between items-center">
                        <a href="{{ url_for('view_post', post_id=post.id) }}" class="text-primary hover:underline">
                            阅读全文 <i class="fa fa-arrow-right ml-1"></i>
                        </a>
                        <div class="flex space-x-2">
                            {% if current_user.is_authenticated and (not post.user_id or post.user_id == current_user.id) %}
                            <a href="{{ url_for('edit_post', post_id=post.id) }}" class="text-gray-500 hover:text-primary transition-colors">
                                <i class="fa fa-pencil"></i>
                            </a>
                            <form action="{{ url_for('delete_post', post_id=post.id) }}" method="POST" class="inline" onsubmit="return confirm('确定要删除这篇文章吗？')">
                                <button type="submit" class="text-gray-500 hover:text-danger transition-colors">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </form>
                            {% endif %}
                        </div>
                    </div>
                </article>
                {% else %}
                <div class="text-center py-16 bg-white rounded-lg shadow-md">
                    <i class="fa fa-file-text-o text-5xl text-gray-300 mb-4"></i>
                    <h3 class="text-xl font-medium text-gray-600 mb-2">还没有博客文章</h3>
                    <p class="text-gray-500 mb-6">点击"写新文章"按钮创建您的第一篇博客</p>
                    <a href="{{ url_for('new_post') }}" class="btn btn-primary inline-flex items-center">
                        <i class="fa fa-plus-circle mr-2"></i>写新文章
                    </a>
                </div>
                {% endfor %}
            </div>
        </div>
    </div>

    <footer class="bg-dark text-white py-8 mt-auto">
        <div class="container mx-auto px-4 max-w-4xl text-center">
            <p>© {{ now.year }} 个人博客系统</p>
        </div>
    </footer>

    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
        <div class="fixed bottom-4 right-4 z-50">
            {% for category, message in messages %}
            <div class="{{ 'bg-green-500' if category == 'success' else 'bg-red-500' }} text-white px-6 py-4 rounded-lg shadow-lg mb-3 flex items-center">
                <i class="fa {{ 'fa-check-circle' if category == 'success' else 'fa-exclamation-circle' }} mr-3"></i>
                <span>{{ message }}</span>
                <button class="ml-auto text-white hover:text-gray-200" onclick="this.parentElement.remove()">
                    <i class="fa fa-times"></i>
                </button>
            </div>
            {% endfor %}
        </div>
        {% endif %}
    {% endwith %}

    <script>
        // 添加当前年份到页脚
        document.addEventListener('DOMContentLoaded', function() {
            const footerYear = document.querySelector('footer p');
            if (footerYear) {
                footerYear.innerHTML = footerYear.innerHTML.replace('{{ now.year }}', new Date().getFullYear());
            }
        });
    </script>
</body>
</html>